//
//  StepReducer.m
//  TestOCRedux
//
//  Created by Hotacool on 2019/2/28.
//  Copyright © 2019 Hotacool. All rights reserved.
//

#import "StepReducer.h"
#import "AppActions.h"
#import "StepRecord.h"

HReducer stepReducer(void) {
    HReducer reducer = ^id<HStateType>(id<HActionType> action, id<HStateType> state){
        StepRecord *sr;
        if ([action.type isEqualToString:kRecordAction]) {
            if (!action.payload) {
                return nil;
            }
            if (state) {
                sr = [((StepRecord*)state) copy];
            } else {
                sr = [StepRecord new];
            }
            NSMutableData *data = [[NSMutableData alloc] init];
            NSKeyedArchiver *keyedDecoder = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
            [action.payload encodeWithCoder:keyedDecoder];
            [keyedDecoder finishEncoding];
            
            [sr.steps addObject:data];
        }
        return sr;
    };
    return reducer;
}
